﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;
using Customer_Profile.Admin;

namespace Customer_Profile.eCard.Reward
{
    public partial class entity_reward_select : System.Web.UI.Page
    {
        string strUserName = "";
        string strUserID = "";
        string strEntityID = "";
        string strEntity = "";

        public string strScript = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntityID = ((UserInfoEntity)Session["usermodel"]).UserEntityID;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }                      

            this.txtSumAmount.Attributes.Add("readonly", "readonly");
            if (!Page.IsPostBack)
            {
                DataSet dsEntity = EntityDao.getEntityList("N");
                dropEntity.DataSource = dsEntity;
                dropEntity.DataTextField = "entity_name";
                dropEntity.DataValueField = "entity_id";
                dropEntity.DataBind();                
                dropEntity.Items.FindByValue(strEntityID).Selected = true;
                dropEntity.Enabled = false;

                Comm.BindDll(ref dropIfreward, "RewardStatus", false, "");

                txtcreatedatebegin.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01";
                string strYear = txtcreatedatebegin.Text.Substring(0, 4);
                string strMonth = txtcreatedatebegin.Text.Substring(5, 2);
                txtcreatedateend.Text = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
                dropYear.Items.FindByValue(strYear).Selected = true;
                dropMonth.Items.FindByValue(strMonth).Selected = true;
            }
            bindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            
        }

        private void bindGrid()
        {
            string Entity_ID = dropEntity.SelectedItem.Value.Trim();
            string strStatus = dropIfreward.SelectedItem.Value;
            string strStartDate = txtcreatedatebegin.Text.Trim();
            string strEndDate = txtcreatedateend.Text.Trim();

            if (strStartDate.Equals("") || strEndDate.Equals(""))
            {
                Response.Write("起始时间和结束时间不能为空");
            }

            DataSet ds = RewardDao.RewardSearch(Entity_ID, strStatus, strStartDate, strEndDate);
            gridReward.DataSource = ds;
            gridReward.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string dealno_list =hidDealno.Value.ToString();
            //Response.Redirect("reward_application.aspx?dealnolist=" + dealno_list);
            bool bok = RewardDao.RewardInsert(dealno_list,strEntityID, strEntity, decimal.Parse(txtSumAmount.Text),dropYear.SelectedItem.Value, dropMonth.SelectedItem.Value, txtRewardBankAccount.Text,txtStaffno.Text.Trim(),txtAccountname.Text.Trim(),strUserID, strUserName, txtRemark.Text);
            if (bok)
            {
                strScript = "<script language='javascript' >alert('申请成功，请等待总部处理！')</script>";
                bindGrid();
                txtAccountname.Text = "";
                txtRemark.Text = "";
                txtRewardBankAccount.Text = "";
                txtStaffno.Text = "";
                txtSumAmount.Text = "";
            }
            else
            {
                strScript = "<script language='javascript' >alert('申请失败，请联系系统管理员')</script>";
            }
        }
    }
}